package com.jcfk.eam.service.main;

import com.jcfk.eam.domain.po.main.EamWorkOrder;
import com.jcfk.eam.domain.query.main.EamWorkOrderQuery;
import com.jcfk.eam.domain.dto.main.EamWorkOrderDTO;
import com.jcfk.eam.domain.dto.main.EamWorkOrderRecordDTO;
import com.jcfk.eam.domain.po.main.EamWorkOrderRecord;
import com.jcfk.eam.dao.main.EamWorkOrderDao;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;
import com.alibaba.druid.util.StringUtils;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.List;

import java.util.Date;

/**
 * 作业工单
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-09-04
 */
@Service
public class EamWorkOrderService extends BaseService<EamWorkOrderDao, EamWorkOrder> {


    @Autowired
    private EamWorkOrderRecordService eamWorkOrderRecordService;

    /**
     * EamWorkOrder分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamWorkOrderDTO> queryByPage(DataPage<EamWorkOrderDTO> page, EamWorkOrderQuery query) {
        // 分页查询
        query.setOrderNo(StringUtils.isEmpty(query.getOrderNo()) ? query.getOrderNo() : query.getOrderNo() + "%");
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }

    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamWorkOrderDTO>
     */
    public List<EamWorkOrderDTO> getList(EamWorkOrderQuery query) {
        // 查询条件
        QueryWrapper<EamWorkOrder> qw = new QueryWrapper<>();
        qw.lambda().eq(query.getAssetKind() != null, EamWorkOrder::getAssetKind, query.getAssetKind());
        qw.lambda().like(!StrUtil.isEmpty(query.getOrderNo()), EamWorkOrder::getOrderNo, query.getOrderNo());
        qw.lambda().eq(query.getOrderType() != null, EamWorkOrder::getOrderType, query.getOrderType());
        qw.lambda().eq(!StrUtil.isEmpty(query.getBusinessId()), EamWorkOrder::getBusinessId, query.getBusinessId());
        qw.lambda().eq(query.getOrderStatus() != null, EamWorkOrder::getOrderStatus, query.getOrderStatus());
        // 查询数据
        List<EamWorkOrder> list = super.list(qw);

        return convertList(list, EamWorkOrderDTO.class);
    }

    /**
     * 获取根据关联id详情表列表
     *
     * @param query 查询条件
     * @return List<EamWorkOrderDTO>
     */
    public List<EamWorkOrderRecordDTO> getDetails(EamWorkOrderQuery query) {
        // 查询条件
        QueryWrapper<EamWorkOrderRecord> qw = new QueryWrapper<>();
        qw.lambda().eq(!StrUtil.isEmpty(query.getId()), EamWorkOrderRecord::getOrderId, query.getId());

        // 查询数据
        List<EamWorkOrderRecord> list = eamWorkOrderRecordService.list(qw);

        return convertList(list, EamWorkOrderRecordDTO.class);
    }


    /**
     * 判断EamWorkOrder是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamWorkOrderDTO dto) {
        QueryWrapper<EamWorkOrder> queryWrapper = new QueryWrapper<>();
        return RetResult.ok();
    }

    /**
     * 新建工单
     * @param dto
     */
    @Transactional(rollbackFor = Exception.class)
    public void create(EamWorkOrderDTO workOrder,EamWorkOrderRecordDTO orderRecord)
    {

    }

    /**
     * 开始作业
     */
    @Transactional(rollbackFor = Exception.class)
    public void beginWork(EamWorkOrderDTO dto,EamWorkOrderRecordDTO orderRecord){

    }

    /**
     * 完成作业
     * @param dto
     */
    @Transactional(rollbackFor = Exception.class)
    public void finish(EamWorkOrderDTO dto)
    {

    }

    /**
     * 转交
     */
    @Transactional(rollbackFor = Exception.class)
    public void transfer(EamWorkOrderDTO dto){

    }

    /**
     * 插入作业工单
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamWorkOrderDTO dto) {

        // 保存
        EamWorkOrder item = new EamWorkOrder();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);

        // 设置关联主键
        for (EamWorkOrderRecordDTO detail : dto.getDetails()) {
            detail.setOrderId(item.getId());
        }

        // 保存明细表
        eamWorkOrderRecordService.saveBatch(dto.getDetails(), item.getId());
    }


    /**
     * 更新作业工单
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamWorkOrderDTO dto) {

        // 保存
        EamWorkOrder item = new EamWorkOrder();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);

        // 设置关联主键
        for (EamWorkOrderRecordDTO detail : dto.getDetails()) {
            detail.setOrderId(item.getId());
        }
        // 保存明细表
        eamWorkOrderRecordService.saveBatch(dto.getDetails(), item.getId());
    }


    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamWorkOrder item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());
        item.setLastUpdatedUserName(SecurityUtils.getUserName());
    }


    /**
     * 根据主键获取作业工单信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamWorkOrderDTO get(String id) {
        // 查询数据
        EamWorkOrder item = this.baseMapper.selectById(id);
        EamWorkOrderDTO dto = new EamWorkOrderDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }


    /**
     * 根据主键获取作业工单信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteItem(String id) {
        this.removeById(id);
    }
}

